package com.metagurukul.metaspace.actions;

import java.sql.SQLException;

import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.metagurukul.metaspace.controller.SignupForm;
import com.metagurukul.metaspace.models.beans.UserLoginDetail;
import com.metagurukul.metaspace.service.UserService;
import com.metagurukul.metaspace.util.EmailSender;
import com.metagurukul.metaspace.util.PasswordGenerator;
import com.metagurukul.metaspace.validator.Validator;

/**
 * Represents a SignUp Action where if user registers his email id a mail will
 * be sent to him containing his email id & a random password so that any fake
 * person can not be registered. This page redirects to mailSucess page.
 * 
 * @author himanshu
 * 
 */
public class SignupAction extends Action {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException,AddressException {
		SignupForm signupForm = (SignupForm) form;
		String emailId = signupForm.getEmailId();
		PasswordGenerator passwordGenerator = new PasswordGenerator();
		String password = passwordGenerator.getPassword();
		Validator validate = new Validator();
		request.setAttribute("mail", emailId);
		if (!validate.validateEmail(emailId)
				|| (!emailId.endsWith("@metacube.com") && !emailId
						.endsWith("@metagurukul.com"))) {
			ActionMessages errors = new ActionMessages();
			errors.add("emailId", new ActionMessage("error.emailId.invalid"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}
		int flag = insertLoginDetail(emailId, password);
		if (flag > 0) {
			EmailSender emailSender = new EmailSender();
			emailSender.sendPassword(emailId, password);
			return mapping.findForward("success");
		} else if (flag == -1) {
			ActionMessages errors = new ActionMessages();
			errors.add("emailId", new ActionMessage("error.signup.fail"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		} else {
			ActionMessages errors = new ActionMessages();
			errors.add("emailId", new ActionMessage("error.userId.invalid"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}
	}

	public int insertLoginDetail(String emailId, String password)
			throws SQLException {
		UserLoginDetail userLoginDetail = new UserLoginDetail();
		userLoginDetail.setEmailId(emailId);
		userLoginDetail.setPassword(password);
		return UserService.insertUserLoginDetail(userLoginDetail);
	}
}
